/**
 * 
 */
package com.tamtamy.jttamobile;

import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;

/**
 * Implementation of HTTPClient with custom keystore, for non standard certificate (e.g tamtamy.reply.eu CA)
 * @see http://blog.antoine.li/index.php/2010/10/android-trusting-ssl-certificates/
 * 
 * @author mirko
 * @version $Id: JTTAHttpClient.java 78 2011-07-23 15:19:02Z m.sciachero@gmail.com $
 */
public class JTTAHttpClient extends DefaultHttpClient {

	
	public JTTAHttpClient() {
		super();
		JTTAHttpClient.trustAllHosts();
	}
	
	
	

	/**
	 * @param conman
	 * @param params
	 */
	public JTTAHttpClient(ClientConnectionManager conman, HttpParams params) {
		super(conman, params);
		JTTAHttpClient.trustAllHosts();
	}




	/**
	 * @param params
	 */
	public JTTAHttpClient(HttpParams params) {
		super(params);
		JTTAHttpClient.trustAllHosts();
	}




	private static void trustAllHosts() {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return new java.security.cert.X509Certificate[] {};
                }

                public void checkClientTrusted(X509Certificate[] chain,
                                String authType) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain,
                                String authType) throws CertificateException {
                }
        } };

        // Install the all-trusting trust manager
        try {
                SSLContext sc = SSLContext.getInstance("TLS");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
                e.printStackTrace();
        }
	}

	
		
}


